package com.open.capacity.task.service.impl;

import com.open.capacity.common.model.SysTask;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.redis.util.RedisUtil;
import com.open.capacity.task.dao.TaskDao;
import com.open.capacity.task.model.TaskPublishLog;
import com.open.capacity.task.model.TaskType;
import com.open.capacity.task.service.RedisService;
import com.open.capacity.task.service.TaskService;
import com.open.capacity.task.utils.CodeGenerator;
import com.open.capacity.task.utils.GetDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskDao taskDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Integer findTaskCounts() {
        Integer count=taskDao.findTaskCounts();
        return count;
    }

    @Override
    public Map<String, String> setInformation(Map<String, String> task) {
        //根据任务类型自动生成编号,先从redis中读取，若redis中无数据则去数据库中查询
        //获取任务类型（普通或紧急）
        String var=task.get("kind");
        Integer kind=Integer.parseInt(var);

        Integer count=0;
        if(!redisUtil.hasKey("count")){
            count=taskDao.findTaskCounts();
            redisUtil.set("count",count);
        }else {
            count= (Integer) redisUtil.get("count");
            count++;
            redisUtil.set("count",count);
        }

        String tkcode= CodeGenerator.Generate(kind,count);

        //设置任务的状态，默认为1:正常
        int status=1;

        //获取当前日期时间
        String cretim= GetDate.get();

        //设置任务标识，默认为0：正在发布中
        int tkflag=0;

        //设置任务积分
        if(task.get("integr")==null){
            task.put("integr","5");
        }
        else {
            String point=task.get("integr");
            int p=Integer.parseInt(point);
            p=p+5;
            String points= Integer.toString(p);
            task.put("integr",points);
        }
        //填充任务信息
        task.put("tkcode",tkcode);
        task.put("status", Integer.toString(status));
        task.put("cretim",cretim);
        task.put("tkflag",Integer.toString(tkflag));
        return task;
    }

    @Override
    public Integer publishTask(Map<String, String> task) {

        //记录发布日志
        TaskPublishLog log=TaskPublishLog.builder()
                .userid(task.get("userid"))
                .tkcode(task.get("tkcode"))
                .cretim(task.get("cretim"))
                .build();
        taskDao.logPublish(log);

        //将发布的任务存入数据库
        return taskDao.publishTask(task);
    }

    @Override
    public Integer updateTask(Map<String,String> sysTask) {
        return taskDao.updateTask(sysTask);
    }

    @Override
    public Integer addTaskType(Map<String, String> taskType) {
        return taskDao.addTaskType(taskType);
    }

    @Override
    public Map findTaskType(String type) {
        Map taskType=new HashMap();
        taskType=taskDao.findTaskType(type);
        return taskType;
    }

    @Override
    public Integer addTaskLevel(Map<String, Object> type) {
        return taskDao.addTaskLevel(type);
    }

    @Override
    public Map findTaskLevel(String type) {
        return taskDao.findTaskLevel(type);
    }

    @Override
    @Transactional
    public Integer updateTaskLevel(Map<String, Object> type) {
        return taskDao.updateTaskLevel(type);
    }

    @Override
    public Integer updateTaskType(Map<String, String> taskType) {
        return taskDao.updateTaskType(taskType);
    }

    @Override
    @Transactional
    public List<SysTask> findTasks(Map<String, Object> conditions) {
        PageUtil.pageParamConver(conditions,true);
        return taskDao.findTasks(conditions);
    }

    @Override
    public TaskType findTaskAreas(Map<String, Object> conditions) {
        return null;
    }

    @Override
    public int findCounts(Map<String, Object> conditions) {
        return taskDao.count(conditions);
    }
}
